using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using YayiMgr.Ability.Docking.Commom;
using YayiMgr.Ability.Docking.CompanyApp;
using YayiMgr.Ability.Docking.CompanyApp.Dto;
using YayiMgr.Domain.SystemBase;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.CompanyApp
{
    public class CompanyServicer: AbilityServicer ,ICompanyServicer
    {
        public IRepository<Company> CompanyRepo { get; set; }
        
        public async Task<QueryListDto<CompanyDto>> GetAllCompanys()
        {
            List<Company> items = await CompanyRepo.GetListAsync();
            List<CompanyDto> Data = ModelMapper.Map<List<Company>, List<CompanyDto>>(items);
            int total = await CompanyRepo.CountAsync();
            QueryListDto<CompanyDto> listDto = QueryListDto<CompanyDto>.Create(Data, total, 1, total);
            return listDto;
        }

        //CompanyQueryDto
        public async Task<QueryListDto<CompanyDto>> GetCompanys(CompanyQueryDto dto)
        {
            QueryListDto<CompanyDto> listDto = new QueryListDto<CompanyDto>();
            IQueryable<Company> companies = CompanyRepo.AsQueryable();
            if(!string.IsNullOrWhiteSpace(dto.CompanyName))
                companies = companies.Where(c=>c.CompanyName.Contains(dto.CompanyName));
            if(!string.IsNullOrWhiteSpace(dto.CompanyQysh))
                companies = companies.Where(c=>c.CompanyQysh.Contains(dto.CompanyQysh));
            if(!string.IsNullOrWhiteSpace(dto.LinkMan))
                companies = companies.Where(c=>c.LinkMan.Contains(dto.LinkMan));
            if(!string.IsNullOrWhiteSpace(dto.Phone))
                companies = companies.Where(c=>c.Phone.Contains(dto.Phone));
            if(dto.Level!=null)
                companies = companies.Where(c=>c.Level==dto.Level);
            if(dto.StartDate!=null)
                companies = companies.Where(c=>c.StartDate<=dto.StartDate);
            if(dto.EndDate!=null)
                companies = companies.Where(c=>c.EndDate>=dto.EndDate);

            List<Company> list = await companies.OrderBy(p=>p.CompanyName).Skip((dto.PageIndex-1)*dto.PageSize).Take(dto.PageSize).ToListAsync();
            List<CompanyDto> Data =  ModelMapper.Map<List<Company>,List<CompanyDto>>(list);

            int total = await companies.CountAsync();
            listDto = QueryListDto<CompanyDto>.Create(Data, total, dto.PageIndex, dto.PageSize);
            return listDto;
        }

        public async Task<ResponseDto<CompanyDto>> GetCompanyById(Guid Id)
        {
            Company item = await CompanyRepo.GetAsync(Id);
            if(item==null)
                return ResponseDto<CompanyDto>.CreateFail("公司不存在");
            CompanyDto dto = ModelMapper.Map<Company, CompanyDto>(item);
            return ResponseDto<CompanyDto>.CreateSuccess(dto);
        }

        public async Task<ResponseDto<CompanyDto>> AddCompany(CompanyCreateDto dto)
        {
            var item = ModelMapper.Map<CompanyCreateDto, Company>(dto);

            item = await CompanyRepo.InsertAsync(item);
            var itemDto = ModelMapper.Map<Company, CompanyDto>(item);
            return ResponseDto<CompanyDto>.CreateSuccess(itemDto);
        }

        public async Task<ResponseDto<CompanyDto>> UpdateCompany(Guid Id, CompanyUpdateDto dto)
        {
            Company item = await CompanyRepo.GetAsync(Id);
            if(item==null)
                return ResponseDto<CompanyDto>.CreateFail("公司不存在");
            item = ModelMapper.Map<CompanyUpdateDto, Company>(dto);
            item = await CompanyRepo.UpdateAsync(item);
            CompanyDto dto1 = ModelMapper.Map<Company,CompanyDto>(item);
            return ResponseDto<CompanyDto>.CreateSuccess(dto1);
        }

        public async Task DeleteCompany(Guid Id)
        {
            await CompanyRepo.DeleteAsync(Id);
        }  
    }
}